
indexModule.controller('adList',function($scope,$http,$modal,$routeParams){
    //网站id
    $scope.id = $routeParams.id;

    //从cookie或者网站名称和地址
    $scope.webName = $.cookie("webName");
    $scope.webUrl = $.cookie("webUrl");

    //分页
    $scope.page = {
        page: 1,//当前页数
        limit: 10,// 每页显示多少条
        maxSize: 7 // 显示多少个分页按钮
    };

    //广告类型
    $scope.adTypes = [
        {name:"请选择",value:""},
        {name:"360",value:"360"},
        {name:"搜狗",value:"sogou"},
        {name:"百度",value:"baidu"}
    ];
    $scope.ad_type = $scope.adTypes[0].value;
    $scope.search_type = $scope.adTypes[0].value;

    //请求用户list
    var postData = {wid:$scope.id,page:$scope.page.page,size:$scope.page.limit};
    adlist(postData,$scope.page.page);
    function adlist(postData,page){
        postHttp($http,config.adList_httpUrl,postData,function(data){
            $scope.tableData = data.data.list;
            $scope.page.page = page;
            $scope.page.total = data.data.total;// list总数量
            $scope.page.numPages = Math.ceil(data.data.total/$scope.page.limit); // 总页数， 101/20 =6, 这个分页那个自动计算的值。
        });
    };

    //点击搜索
    var hasSearch = false;
    $scope.search = function(){
        var listData = {wid:$scope.id,name:$scope.search_name,start:$("#search_start").val(),end:$("#search_end").val(),type:$scope.search_type,page:1,size:$scope.page.limit};
        adlist(listData,1);
        hasSearch = true;
    };

    //页码改变时
    $scope.pageChanged = function(){
        if(typeof(localStorage) === "object"){
            localStorage.setItem("limit",$scope.page.limit);
        };
        if(parseInt($scope.page.page) > parseInt($scope.page.numPages)){
            $scope.page.page = parseInt($scope.page.numPages);
        }else if(parseInt($scope.page.page) < 1){
            $scope.page.page = 1;
        };
        if(hasSearch){
            var postData = {wid:$scope.id,name:$scope.search_name,start:$("#search_start").val(),end:$("#search_end").val(),type:$scope.search_type,page:$scope.page.page,size:$scope.page.limit};
        }else{
            var postData = {wid:$scope.id,page:$scope.page.page,size:$scope.page.limit};
        }
        adlist(postData,$scope.page.page);
    };

    //新增广告词
    $scope.add = function(){
        var name = $scope.add_ad;
        var type = $scope.ad_type;
        if(!name){
            return alert(config.adMiss_nameVal);
        }else if(!type){
            return alert(config.adMiss_typeVal);
        }else{
            var postData = {name:name,type:type,wid:$scope.id};
            postHttp($http,config.adSave_httpUrl,postData,function(data){
                alert(config.saveHttpMsg);
                $scope.add_ad = "";
                var postData = {wid:$scope.id,page:1,size:$scope.page.limit};
                adlist(postData,$scope.page.page);
            });
        }
    };

    //分享广告词
    $scope.share = function(id,share,index){
        if($scope.tableData[index].price){
            if(parseInt(share) != 1){
                var postData = {id:id};
                postHttp($http,config.adShare_httpUrl,postData,function(data){
                    alert(config.shareHttpMsg);
                    window.location.reload();
                });
            };
        }else{
            return alert("必须要输入单价！");
        }
    };

    //修改广告词
    $scope.modify = function(index){
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: '/views/ad/save.html',
            controller: 'saveAdModal',
            resolve: {
                items: function () {
                    return $scope.tableData[index];
                }
            }
        });
    };

    //删除广告词
    $scope.delete = function(id){
        var postData = {id:id};
        postHttp($http,config.adDel_httpUrl,postData,function(data){
            alert(config.delHttpMsg);
            window.location.reload();
        });
    };

    //查询今天0/昨天-1/前天-2的数据
    $scope.dateTab = [true,false,false];
    $scope.dateData = function(index){
        for(var i= 0;i < $scope.dateTab.length;i++){
            if(Math.abs(index) === i){
                $scope.dateTab[i] = true;
            }else{
                $scope.dateTab[i] = false;
            }
        };
        var date = getDateString(index);
        var postData = {wid:$scope.id,page:$scope.page.page,size:$scope.page.limit,start:date};
        adlist(postData,$scope.page.page);
    };

    //返回上一页
    $scope.reback = function(){
        window.history.go(-1);
    };
});


indexModule.controller('saveAdModal', function ($scope,$http,$modalInstance,items,$routeParams) {
    //网站id
    $scope.id = $routeParams.id;

    //广告类型
    $scope.adTypes = [
        {name:"请选择",value:""},
        {name:"360",value:"360"},
        {name:"搜狗",value:"sogou"},
        {name:"百度",value:"baidu"}
    ];

    $scope.name = items.name;
    for(var i in $scope.adTypes){
        if(items.type === $scope.adTypes[i].value){
            $scope.modify_type = $scope.adTypes[i].value;
        }
    }
    $scope.price = items.price || "";
    var reg = /^\d+(?=\.{0,1}\d+$|$)/;
    $scope.ok = function () {
        //提交添加
        var name = $scope.name;
        var type = $scope.modify_type;
        var price = $scope.price;
        if(!name&& type === ""){
            $scope.modify_tips = config.modifyAdTipHttpMsg;
        }else if($scope.price != "" && !reg.test($scope.price)){
            return alert(config.mustNum);
        }else{
            $scope.add_tips = config.saveTipHttpMsg;
            var postData = {id:items._id,name:name,type:$scope.modify_type,price:price,wid:$scope.id};
            postHttp($http,config.adSave_httpUrl,postData,function(data){
                alert(config.saveHttpMsg);
                $modalInstance.close('ok');
                window.location.reload();
            });
        }
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});
